#pragma once
//显示器

#include "ipc/mutex.h"
#include "dev/tty.h"
#include "comm/types.h"

#define CONSOLE_DISP_ADDR (0xb8000)              //显存起始地址
#define CONSOLE_DISP_END  (0xb8000+32*1024)      //显存结束地址

#define CONSOLE_ROW_MAX   25
#define CONSOLE_COL_MAX   80

#define	ESC_PARAM_MAX				10			// 最多支持的ESC [ 参数数量


#define ASCII_ESC 0x1b   // \033


//各种颜色
typedef enum _cclor_t 
{
    COLOR_Black			= 0,
    COLOR_Blue			= 1,
    COLOR_Green			= 2,
    COLOR_Cyan			= 3,
    COLOR_Red			= 4,
    COLOR_Magenta		= 5,
    COLOR_Brown			= 6,
    COLOR_Gray			= 7,
    COLOR_Dark_Gray 	= 8,
    COLOR_Light_Blue	= 9,
    COLOR_Light_Green	= 10,
    COLOR_Light_Cyan	= 11,
    COLOR_Light_Red		= 12,
    COLOR_Light_Magenta	= 13,
    COLOR_Yellow		= 14,
    COLOR_White			= 15
}color_t;



typedef union _disp_char_t
{
    struct
    {
        char c;                   //字符
        char foreground : 4;      //前景色
        char background : 3;      //背景色
    };
    
    uint16_t v;
}disp_char_t;


typedef struct __console_t
{
    enum
    {
        CONSOLE_WRITE_NORMAL,
        CONSOLE_WRITE_ESC,
        CONSOLE_WRITE_SQUARE,           // ESC [接收状态
    }write_state;


    disp_char_t* disp_base;
    int display_rows;
    int display_cols;

    //光标位置
    int cursor_col;
    int cursor_row;

    int old_cursor_col;
    int old_cursor_row;


    color_t foreground, background;	// 前后景色

    int esc_param[ESC_PARAM_MAX];	// ESC [ ;;参数数量
    int curr_param_index;

    mutex_t mutex;

}console_t;




int console_init(int idx);
int console_write(tty_t* tty);
void console_close(int console);

void console_select(int idx);